//
//  AppDelegate.swift
//  WeiBlog
//
//  Created by xiang on 15/10/15.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    /// 是否新版本
    var isNewVersion:Bool{
        
        let currentVersion = NSUserDefaults.standardUserDefaults().doubleForKey("version")
        
        let newVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
        
        NSUserDefaults.standardUserDefaults().setDouble(newVersion!, forKey: "version")
        
        return currentVersion < newVersion
    }
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        print(UserAccountViewModel.sharedUserAccountViewModel.userAccountInfo?.access_token)
        //新版本跳到新特性
        
        if isNewVersion {
            window?.rootViewController = NewFeatureViewController()
        }else{
            //第一次运行时
            window?.rootViewController = UserAccountViewModel.sharedUserAccountViewModel.isLogin ? WelcomeViewController() : MainTabBarController()
        }
        
        
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: "jumpToVC", name: "WBJumpNotification", object: nil)
//        NSNotificationCenter.defaultCenter().postNotificationName("WBJumpNotification", object: 这个地方表示要传递的内容)
        NSNotificationCenter.defaultCenter().addObserverForName(
            "WBJumpToNotification",       //通知名称
            object: nil,                //发送通知的对象,nil,表示接受任何对象的通知
            queue: nil)                 //执行队列nil表示主队列
            { [weak self] (notification) -> Void in //接收到通知执行的代码
                if notification.object != nil{
                    self?.window?.rootViewController = WelcomeViewController()
                }else{
                    
                    self?.window?.rootViewController = MainTabBarController()
                }
        }
        
        window?.makeKeyAndVisible()
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        return true
    }
    
    
    deinit {
        // 注销通知 - 注销指定的通知
        NSNotificationCenter.defaultCenter().removeObserver(self,   // 监听者
            name: "WBJumpToNotification",           // 监听的通知
            object: nil)                                            // 发送通知的对象
    }
    
}












